//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>
package org.jpgolay.gui.slideShow;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintStream;
import javax.swing.*;
import javax.swing.border.BevelBorder;

public class SlideShow extends JFrame
        implements ActionListener {

    public SlideShow() {
        initComponents();
        count = -1;
        timer = new Timer(4000, this);
    }

    private void initComponents() {
        slideShow = new JPanel();
        screenP = new JPanel();
        backgroundP = new JPanel();
        screenL = new JLabel();
        buttonP = new JPanel();
        firstOneB = new JButton();
        lastB = new JButton();
        stopB = new JButton();
        playB = new JButton();
        nextB = new JButton();
        lastOneB = new JButton();
        jPanel1 = new JPanel();
        jPanel2 = new JPanel();
        jPanel3 = new JPanel();
        setTitle("DIAPORAMA");
        slideShow.setLayout(new BorderLayout());
        slideShow.setBorder(new BevelBorder(0));
        screenP.setLayout(new BorderLayout());
        screenP.setBorder(new BevelBorder(1));
        backgroundP.setLayout(new GridBagLayout());
        screenL.setHorizontalAlignment(0);
        screenL.setIcon(new ImageIcon(""));
        screenL.setText("  ");
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.fill = 1;
        gridBagConstraints.weightx = 1.0D;
        gridBagConstraints.weighty = 1.0D;
        backgroundP.add(screenL, gridBagConstraints);
        screenP.add(backgroundP, "Center");
        slideShow.add(screenP, "Center");
        buttonP.setLayout(new FlowLayout(2));
        firstOneB.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/debut.png")));
        firstOneB.setToolTipText("DEBUT");
        firstOneB.setEnabled(false);
        firstOneB.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                firstOneBActionPerformed(evt);
            }
        });
        buttonP.add(firstOneB);
        lastB.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/retour.png")));
        lastB.setToolTipText("RETOUR");
        lastB.setEnabled(false);
        lastB.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                lastBActionPerformed(evt);
            }
        });
        buttonP.add(lastB);
        stopB.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/stop.png")));
        stopB.setToolTipText("STOP");
        stopB.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                stopBActionPerformed(evt);
            }
        });
        buttonP.add(stopB);
        playB.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/play.png")));
        playB.setToolTipText("LECTURE");
        playB.setEnabled(false);
        playB.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                playBActionPerformed(evt);
            }
        });
        buttonP.add(playB);
        nextB.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/avance.png")));
        nextB.setToolTipText("AVANCE");
        nextB.setEnabled(false);
        nextB.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                nextBActionPerformed(evt);
            }
        });
        buttonP.add(nextB);
        lastOneB.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/fin.png")));
        lastOneB.setToolTipText("FIN");
        lastOneB.setEnabled(false);
        lastOneB.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                lastOneBActionPerformed(evt);
            }
        });
        buttonP.add(lastOneB);
        slideShow.add(buttonP, "South");
        slideShow.add(jPanel1, "North");
        slideShow.add(jPanel2, "East");
        slideShow.add(jPanel3, "West");
        getContentPane().add(slideShow, "Center");
        pack();
    }

    private void lastOneBActionPerformed(ActionEvent evt) {
        lastOneImage();
    }

    private void firstOneBActionPerformed(ActionEvent evt) {
        firstOneImage();
    }

    private void nextBActionPerformed(ActionEvent evt) {
        nextImage();
    }

    private void playBActionPerformed(ActionEvent evt) {
        startShow();
    }

    private void stopBActionPerformed(ActionEvent evt) {
        stopShow();
    }

    private void lastBActionPerformed(ActionEvent evt) {
        lastImage();
    }

    private ImageIcon resizeImageIcon(ImageIcon tmpIcon) {
        if (tmpIcon.getIconWidth() > screenL.getWidth()) {
            return new ImageIcon(tmpIcon.getImage().getScaledInstance(screenL.getWidth(), -1, 1));
        } else {
            return tmpIcon;
        }
    }

    private void updateImage() {
        try {
            screenL.setIcon(resizeImageIcon(new ImageIcon((byte[]) images[count])));
        } catch (Exception e) {
            timer.stop();
            System.out.println("Impossible de charger une image : ");
        }
    }

    private void firstOneImage() {
        count = 0;
        updateImage();
    }

    private void lastOneImage() {
        count = images.length - 1;
        updateImage();
    }

    private void nextImage() {
        count++;
        if (count == images.length) {
            count = 0;
        }
        updateImage();
    }

    private void lastImage() {
        count--;
        if (count == -1) {
            count = images.length - 1;
        }
        updateImage();
    }

    private void stopShow() {
        timer.stop();
        setEnabledButtons(true);
    }

    private void startShow() {
        setEnabledButtons(false);
        timer.start();
    }

    private void setEnabledButtons(boolean b) {
        lastB.setEnabled(b);
        playB.setEnabled(b);
        nextB.setEnabled(b);
        firstOneB.setEnabled(b);
        lastOneB.setEnabled(b);
    }

    public void start() {
        startShow();
    }

    public Object[] getImages() {
        return images;
    }

    public void setImages(Object images[]) {
        this.images = images;
    }

    public void actionPerformed(ActionEvent actionEvent) {
        nextImage();
    }
    private JPanel jPanel3;
    private JButton nextB;
    private JButton stopB;
    private JPanel screenP;
    private JPanel jPanel2;
    private JPanel backgroundP;
    private JButton playB;
    private JPanel buttonP;
    private JButton lastB;
    private JLabel screenL;
    private JPanel jPanel1;
    private JButton firstOneB;
    private JPanel slideShow;
    private JButton lastOneB;
    private Object images[];
    private Timer timer;
    private int count;
}
